package eecs285.proj4.gui;

import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;

/**
 * A custom class that extends the Canvas class to make use of its default
 * implementation as well extends its functionality.
 * 
 * @author Easton Thomas
 * @author Stubbed by Vidal Borromeo
 */
@SuppressWarnings("serial")
public class MarkerboardCanvasOLD extends Canvas implements KeyListener,
    MouseListener, MouseMotionListener
{

  private Graphics2D gBuf; // 2D Graphics instance for double buffering
  Image backbuffer = null; // Double Buffer Image
  Image img; // WhiteBoard Image
  // private BufferedImage bi = new BufferedImage(800, 600,
  // BufferedImage.TYPE_INT_RGB); //For background Image?

  // Declare class instances for each type
  MAction action;
  Circle circle;
  Rectangle rectangle;
  Line line;
  Erase erase;
  FreeDraw freedraw;
  Text text;

  Markerboard parent;

  // Holds type of current action
  public MAction.TYPE type;

  // Holds array of MActions as an undo buffer
  ArrayList<MAction> actionList;

  // Boolean variable to allow paint() to run initial paint jobs
  private Boolean Load = true;
  Boolean eraseFlag = false;
  Boolean importAction = false;

  private String s; // For Text sent to Draw()
  private int height = 600;
  private int width = 800;


  // ---------------------------------------------------------------------------

  public MarkerboardCanvasOLD(Markerboard parent_)
  {

    // For reading in Canvas background image (such as a white board)
    /*
     * try { img = ImageIO .read(new File(
     * "/Users/easton/Documents/workspace/EECS 285 - Project 04 GUI/src/Paper.jpg"
     * )); } catch( IOException e ) { // TODO Auto-generated catch block
     * e.printStackTrace(); }
     */

    parent = parent_;
    this.setSize(width, height);
    setBackground(Color.white);
    addMouseListener(this);
    addMouseMotionListener(this);
    addKeyListener(this);

    actionList = new ArrayList<MAction>();

    type = MAction.TYPE.FREEDRAW; // Temporary hardcoding until buttons arrive
  }

  // ---------------------------------------------------------------------------

  // NOTE: This method is called ANYTIME there are changes to the window,
  // including but not limited to repositioning/resizing. 
  public void paint(Graphics g)
  {

    // First time
    if( backbuffer == null )
    {
      backbuffer = createImage(width, height); // create image to buffer
      // drawings too
      gBuf = (Graphics2D) backbuffer.getGraphics(); // get graphics of buffer
      // gBuf.drawImage(img, 0, 0, this); // Write background image to buffer
    }

    // If not first load, call drawing function for appropriate type
    if( Load.equals(false) )
    {
      if(eraseFlag.equals(true))
      {
          gBuf.setColor(Color.WHITE);
          gBuf.fillRect(0, 0, width, height);
          eraseFlag = false;
      }
      else if(importAction.equals(true))
      {
        action.draw(gBuf);
      }
      else if( type.equals(MAction.TYPE.CIRCLE) )
      {
        if( circle != null )
          circle.draw(gBuf);
      }
      else if( type.equals(MAction.TYPE.RECTANGLE) )
      {
        if( rectangle != null )
          rectangle.draw(gBuf);
      }
      else if( type.equals(MAction.TYPE.LINE) )
      {
        if( line != null )
          line.draw(gBuf);
      }
      else if( type.equals(MAction.TYPE.ERASE) )
      {
        if( erase != null )
          erase.draw(gBuf);
      }
      else if( type.equals(MAction.TYPE.FREEDRAW) )
      {
        if( freedraw != null )
          freedraw.draw(gBuf);
      }
      else if( type.equals(MAction.TYPE.TEXT) )
      {
        if( text != null )
          text.draw(gBuf, s);
      }
    }

    // Set first time flag to false
    Load = false;

    // Draw the buffer image to Canvas
    g.drawImage(backbuffer, 0, 0, this);

  }

  // ---------------------------------------------------------------------------

  public void setType(MAction.TYPE type_)
  {
    type = type_;
  }

  // ---------------------------------------------------------------------------

  public void clearMarkerboard(MAction.TYPE type_)
  {
    type = type_;
  }

  // ---------------------------------------------------------------------------

  public Color getDrawColor()
  {
 // Black
    if( parent.colorChooserComboBox.getSelectedIndex() == 0 )
    {
      return Color.BLACK;
    }
    // Pink
    else if( parent.colorChooserComboBox.getSelectedIndex() == 1 )
    {
      return Color.PINK;
    }
    // Red
    else if( parent.colorChooserComboBox.getSelectedIndex() == 2 )
    {
      return Color.RED;
    }
    // Orange
    else if( parent.colorChooserComboBox.getSelectedIndex() == 3 )
    {
      return Color.ORANGE;
    }
    // Yellow
    else if( parent.colorChooserComboBox.getSelectedIndex() == 4 )
    {
      return Color.YELLOW;
    }
    // Green
    else if( parent.colorChooserComboBox.getSelectedIndex() == 5 )
    {
      return Color.GREEN;
    }
    // Cyan
    else if( parent.colorChooserComboBox.getSelectedIndex() == 6 )
    {
      return Color.CYAN;
    }
    // Blue
    else if( parent.colorChooserComboBox.getSelectedIndex() == 7 )
    {
      return Color.BLUE;
    }
    // Magenta
    else if( parent.colorChooserComboBox.getSelectedIndex() == 8 )
    {
      return Color.MAGENTA;
    }
    // Gray
    else if( parent.colorChooserComboBox.getSelectedIndex() == 9 )
    {
      return Color.GRAY;
    }
    else
    {
      return Color.BLACK;
    }
    
  }
  
  public int getDrawStroke()
  {
    return Integer.parseInt(parent.strokeChooserTextField.getText());
  }

  // ---------------------------------------------------------------------------

  public void mousePressed(MouseEvent e)
  {
    // getType from button and put in action.type

    if( e.getX() < width && e.getY() < height ) // check if mouse is in Canvas
    // area
    {
      if( type.equals(MAction.TYPE.CIRCLE) )
      {
        circle = new Circle(new Point(e.getX(), e.getY()));
        circle.setStrokeColor(getDrawColor());
        circle.setStroke(getDrawStroke());
      }
      else if( type.equals(MAction.TYPE.RECTANGLE) )
      {
        rectangle = new Rectangle(new Point(e.getX(), e.getY()));
        rectangle.setStrokeColor(getDrawColor());
        rectangle.setStroke(getDrawStroke());
      }
      else if( type.equals(MAction.TYPE.LINE) )
      {
        line = new Line(new Point(e.getX(), e.getY()));
        line.setStrokeColor(getDrawColor());
        line.setStroke(getDrawStroke());
      }
      else if( type.equals(MAction.TYPE.FREEDRAW) )
      {
        freedraw = new FreeDraw();
        freedraw.setStrokeColor(getDrawColor());
        freedraw.setStroke(getDrawStroke());
        freedraw.points.add(new Point(e.getX(), e.getY()));
      }
      else if( type.equals(MAction.TYPE.ERASE) )
      {
        erase = new Erase();
        erase.setStrokeColor(getDrawColor());
        erase.setStroke(getDrawStroke());
        erase.points.add(new Point(e.getX(), e.getY()));
      }
      else if( type.equals(MAction.TYPE.TEXT) )
      {
        text = new Text(new String(""), new Point(e.getX(), e.getY()));
        text.setFontSize(getDrawStroke());
        text.setFontByName("Helvetica");
        text.setStrokeColor(getDrawColor());
        text.setStroke(getDrawStroke());
        s = "";
      }
    }
  }

  // ---------------------------------------------------------------------------

  public void mouseReleased(MouseEvent e)
  {

    if( e.getX() < width && e.getY() < height ) // check if mouse is in Canvas
    // area
    {
      if( type.equals(MAction.TYPE.CIRCLE) )
      {
        circle.setEnd(new Point(e.getX(), e.getY()));
        repaint();
        // place circle class into parent class instance for buffer
        action = circle;
        actionList.add(action); // add parent class instance to buffer
        // TODO:Send action
      }
      else if( type.equals(MAction.TYPE.RECTANGLE) )
      {
        rectangle.setEnd(new Point(e.getX(), e.getY()));
        repaint();
        // place circle class into parent class instance for buffer
        action = rectangle;
        actionList.add(action);
        // TODO:Send action
      }
      else if( type.equals(MAction.TYPE.LINE) )
      {
        line.setEnd(new Point(e.getX(), e.getY()));
        repaint();
        // place line class into parent class instance for buffer
        action = line;
        actionList.add(action);
        // TODO:Send action
      }
      else if( type.equals(MAction.TYPE.FREEDRAW) )
      {
        repaint();
        // place freedraw class into parent class instance for buffer
        action = freedraw;
        actionList.add(action);
        // TODO:Send action
      }
      else if( type.equals(MAction.TYPE.ERASE) )
      {
        repaint();
        // place erase class into parent class instance for buffer
        action = erase;
        actionList.add(action);
        // TODO:Send action
      }
      
      //Broadcast action.
      try
      {
        parent.connection.broadcast("ActionObject", action);
      }
      catch( IOException e1 )
      {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }

    }
  }

  // ---------------------------------------------------------------------------

  public void mouseDragged(MouseEvent e)
  {
    if( type.equals(MAction.TYPE.FREEDRAW) )
    {
      // add new point if mouse moves and repaint
      freedraw.points.add(new Point(e.getX(), e.getY()));
      repaint();
    }
    else if( type.equals(MAction.TYPE.ERASE) )
    {
      // add new point if mouse moves and repaint
      erase.points.add(new Point(e.getX(), e.getY()));
      repaint();
    }
  }

  // ---------------------------------------------------------------------------

  public void keyTyped(KeyEvent e)
  {
    // If type tool is selected
    if( type.equals(MAction.TYPE.TEXT) )
    {
      // get character
      char c = e.getKeyChar();
      
      if(c == KeyEvent.VK_ENTER)
      {
        try
        {
          parent.connection.broadcast("ActionObject", action);
        }
        catch( IOException e1 )
        {
          // TODO Auto-generated catch block
          e1.printStackTrace();
        }
      }
      else if( c != KeyEvent.CHAR_UNDEFINED )
      {
        s = s + c; // append new character to string
        repaint(); // paint string to canvas
        e.consume();
      }
    }
  }

  // ---------------------------------------------------------------------------

  public void mouseMoved(MouseEvent e)
  {

  }

  public void mouseClicked(MouseEvent e)
  {

  }

  public void mouseEntered(MouseEvent e)
  {

  }

  public void mouseExited(MouseEvent e)
  {

  }

  public void keyPressed(KeyEvent e)
  {

  }

  public void keyReleased(KeyEvent e)
  {

  }

}// MarkerboardCanvasOLD
